/*
	Copyright (c) 2004 Cory Nelson

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be included
	in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gamestat.h"
#include "internal.h"

typedef struct {
	unsigned char check[6];
	unsigned char data[1];
} TRIBES2_PACKET;

static char *tribes2_parse_string(unsigned char **data) {
	unsigned char len=(*data)[0];
	char *str=malloc(len+1);

	(*data)++;
	memcpy(str, *data, len); (*data)+=len;
	str[len]='\0';

	return str;
}

static void tribes2_skip_string(unsigned char **data) {
	unsigned char len=(*data)[0];
	(*data)+=len+1;
}

GS_SERVERINFO *queryserver_tribes2(const char *host, unsigned short port) {
	SOCKET s=getsockudp(host, port);
	GS_SERVERINFO *info;

	if(s==INVALID_SOCKET) return NULL;

	info=calloc(1, sizeof(GS_SERVERINFO));

	//////////////////////////////////////////////////////////////
	// Get Info
	{
		size_t packetlen;
		TRIBES2_PACKET *packet;
		unsigned char *iter;
		clock_t ping;

		ping=clock();
		if(send(s, "\x0E\x02\x01\x02\x03\x04", 6, 0)==SOCKET_ERROR) {
			lasterror=GS_ERROR_TIMEOUT;
			goto err;
		}
		packet=(TRIBES2_PACKET*)getpacket(s, &packetlen);
		if(!packet) {
			lasterror=GS_ERROR_TIMEOUT;
			goto err;
		}
		ping=clock()-ping;

		info->ping=(int)ping;

		dbg_dumpbuf("tribes2_info.bin", packet, packetlen);

		if(memcmp(packet->check, "\x10\x02\x01\x02\x03\x04", 6)) {
			free(packet);
			lasterror=GS_ERROR_CORRUPT;
			goto err;
		}

		iter=packet->data;
		tribes2_skip_string(&iter); // query version
		iter+=4; // protocol version
		iter+=4; // min protocol version
		iter+=4; // game build
		info->host=tribes2_parse_string(&iter);

		free(packet);
	}

	//////////////////////////////////////////////////////////////
	// Get Details
	{
		size_t packetlen;
		TRIBES2_PACKET *packet;
		unsigned char *iter;

		if(send(s, "\x12\x02\x01\x02\x03\x04", 6, 0)==SOCKET_ERROR) {
			lasterror=GS_ERROR_TIMEOUT;
			goto err;
		}
		packet=(TRIBES2_PACKET*)getpacket(s, &packetlen);
		if(!packet) {
			lasterror=GS_ERROR_TIMEOUT;
			goto err;
		}

		dbg_dumpbuf("tribes2_details.bin", packet, packetlen);

		if(memcmp(packet->check, "\x14\x02\x01\x02\x03\x04", 6)) {
			free(packet);
			lasterror=GS_ERROR_CORRUPT;
			goto err;
		}

		iter=packet->data;
		info->mod=tribes2_parse_string(&iter);
		tribes2_skip_string(&iter); // game type
		info->map=tribes2_parse_string(&iter);
		info->passworded=((iter[0]&2) == 2); iter++; // flags: dedicated, password etc.

		info->numplayers=iter[0]; iter++;
		info->maxplayers=iter[0]; iter++;
		/*iter++; // bot count
		iter+=2; //	cpu speed
		tribes2_skip_string(&iter); // some info.*/

		free(packet);
	}
	closesocket(s);

	lasterror=GS_OK;
	return info;

err:
	closesocket(s);
	GSFreeServerInfo(info);
	return NULL;
}
